home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ByteArrayInputStream.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  207 lines

  1. /*
  2.  * @(#)ByteArrayInputStream.java    1.22 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class allows an application to create an input stream in 
  19.  * which the bytes read are supplied by the contents of a byte array. 
  20.  * Applications can also read bytes from a string by using a 
  21.  * <code>StringBufferInputStream</code>. 
  22.  *
  23.  * @author  Arthur van Hoff
  24.  * @version 1.22, 07/01/98
  25.  * @see     java.io.StringBufferInputStream
  26.  * @since   JDK1.0
  27.  */
  28. public
  29. class ByteArrayInputStream extends InputStream {
  30.     /**
  31.      * The byte array containing the data. 
  32.      *
  33.      * @since   JDK1.0
  34.      */
  35.     protected byte buf[];
  36.  
  37.     /**
  38.      * The index of the next character to read from the input stream buffer.
  39.      *
  40.      * @since   JDK1.0
  41.      */
  42.     protected int pos;
  43.  
  44.     /**
  45.      * The currently marked position in the stream.
  46.      * ByteArrayInputStreams are marked at position zero by
  47.      * default when constructed.  They may be marked at another
  48.      * position within the buffer by the <code>mark()</code> method.
  49.      * The current buffer position is set to this point by the
  50.      * <code>reset()</code> method.
  51.      *
  52.      * @since   JDK1.1
  53.      */
  54.     protected int mark = 0;
  55.  
  56.     /**
  57.      * The index one greater than the last valid character in the input 
  58.      * stream buffer. 
  59.      *
  60.      * @since   JDK1.0
  61.      */
  62.     protected int count;
  63.  
  64.     /**
  65.      * Creates a new byte array input stream that reads data from the 
  66.      * specified byte array. The byte array is not copied. 
  67.      *
  68.      * @param   buf   the input buffer.
  69.      * @since   JDK1.0
  70.      */
  71.     public ByteArrayInputStream(byte buf[]) {
  72.     this.buf = buf;
  73.         this.pos = 0;
  74.     this.count = buf.length;
  75.     }
  76.  
  77.     /**
  78.      * Creates a new byte array input stream that reads data from the 
  79.      * specified byte array. Up to <code>length</code> characters are to 
  80.      * be read from the byte array, starting at the indicated offset. 
  81.      * <p>
  82.      * The byte array is not copied. 
  83.      *
  84.      * @param   buf      the input buffer.
  85.      * @param   offset   the offset in the buffer of the first byte to read.
  86.      * @param   length   the maximum number of bytes to read from the buffer.
  87.      * @since   JDK1.0
  88.      */
  89.     public ByteArrayInputStream(byte buf[], int offset, int length) {
  90.     this.buf = buf;
  91.         this.pos = offset;
  92.     this.count = Math.min(offset + length, buf.length);
  93.     }
  94.  
  95.     /**
  96.      * Reads the next byte of data from this input stream. The value 
  97.      * byte is returned as an <code>int</code> in the range 
  98.      * <code>0</code> to <code>255</code>. If no byte is available 
  99.      * because the end of the stream has been reached, the value 
  100.      * <code>-1</code> is returned. 
  101.      * <p>
  102.      * The <code>read</code> method of <code>ByteArrayInputStream</code> 
  103.      * cannot block. 
  104.      *
  105.      * @return  the next byte of data, or <code>-1</code> if the end of the
  106.      *          stream has been reached.
  107.      * @since   JDK1.0
  108.      */
  109.     public synchronized int read() {
  110.     return (pos < count) ? (buf[pos++] & 0xff) : -1;
  111.     }
  112.  
  113.     /**
  114.      * Reads up to <code>len</code> bytes of data into an array of bytes 
  115.      * from this input stream. This <code>read</code> method cannot block. 
  116.      *
  117.      * @param   b     the buffer into which the data is read.
  118.      * @param   off   the start offset of the data.
  119.      * @param   len   the maximum number of bytes read.
  120.      * @return  the total number of bytes read into the buffer, or
  121.      *          <code>-1</code> if there is no more data because the end of
  122.      *          the stream has been reached.
  123.      * @since   JDK1.0
  124.      */
  125.     public synchronized int read(byte b[], int off, int len) {
  126.     if (pos >= count) {
  127.         return -1;
  128.     }
  129.     if (pos + len > count) {
  130.         len = count - pos;
  131.     }
  132.     if (len <= 0) {
  133.         return 0;
  134.     }
  135.     System.arraycopy(buf, pos, b, off, len);
  136.     pos += len;
  137.     return len;
  138.     }
  139.  
  140.     /**
  141.      * Skips <code>n</code> bytes of input from this input stream. Fewer 
  142.      * bytes might be skipped if the end of the input stream is reached. 
  143.      *
  144.      * @param   n   the number of bytes to be skipped.
  145.      * @return  the actual number of bytes skipped.
  146.      * @since   JDK1.0
  147.      */
  148.     public synchronized long skip(long n) {
  149.     if (pos + n > count) {
  150.         n = count - pos;
  151.     }
  152.     if (n < 0) {
  153.         return 0;
  154.     }
  155.     pos += n;
  156.     return n;
  157.     }
  158.  
  159.     /**
  160.      * Returns the number of bytes that can be read from this input 
  161.      * stream without blocking. 
  162.      * <p>
  163.      * The <code>available</code> method of 
  164.      * <code>ByteArrayInputStream</code> returns the value of 
  165.      * <code>count - pos</code>, 
  166.      * which is the number of bytes remaining to be read from the input buffer.
  167.      *
  168.      * @return  the number of bytes that can be read from the input stream
  169.      *          without blocking.
  170.      * @since   JDK1.0
  171.      */
  172.     public synchronized int available() {
  173.     return count - pos;
  174.     }
  175.  
  176.     /**
  177.      * Tests if ByteArrayInputStream supports mark/reset.
  178.      *
  179.      * @since   JDK1.1
  180.      */
  181.     public boolean markSupported() {
  182.     return true;
  183.     }
  184.  
  185.     /**
  186.      * Set the current marked position in the stream.
  187.      * ByteArrayInputStreams are marked at position zero by
  188.      * default when constructed.  They may be marked at another
  189.      * position within the buffer by this method.
  190.      *
  191.      * @since   JDK1.1
  192.      */
  193.     public void mark(int markpos) {
  194.     mark = pos;
  195.     }
  196.  
  197.     /**
  198.      * Resets the buffer to the marked position.  The marked position
  199.      * is the beginning unless another position was marked.
  200.      *
  201.      * @since   JDK1.0
  202.      */
  203.     public synchronized void reset() {
  204.     pos = mark;
  205.     }
  206. }
  207.